package com.example.front.sign;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.front.MyApplication;
import com.example.front.R;
import com.example.front.main.MainActivity;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

/**
 * 注册活动页面类
 */
public class RegisterActivity extends AppCompatActivity {
    private String URL;
    private MyApplication myApplication;
    private EditText account_editText;
    private EditText username_editText;
    private EditText phone_editText;
    private EditText password_editText;
    private EditText password2_editText;
    private Button login_button;
    private Button register_button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        myApplication = (MyApplication)getApplication();
        URL = myApplication.getURL();
        account_editText = findViewById(R.id.account_editText);
        username_editText = findViewById(R.id.username_editText);
        phone_editText = findViewById(R.id.phone_editText);
        password_editText = findViewById(R.id.password_editText);
        password2_editText = findViewById(R.id.password2_editText);
        login_button = findViewById(R.id.login_button);;
        register_button = findViewById(R.id.register_button);
    }

    public void register_onClick(View view){
        String account = account_editText.getText().toString();
        String username = username_editText.getText().toString();
        String phone = phone_editText.getText().toString();
        String password = password_editText.getText().toString();
        String password2 = password2_editText.getText().toString();
        if(account.length()<=0 || username.length()<=0 || phone.length()<=0 || password.length()<=0 || password2.length()<=0){
            Toast.makeText(RegisterActivity.this, "请将信息输入完整", Toast.LENGTH_SHORT).show();
        }
        else if(password.equals(password2)){
            register_button.setText("注册中");
            OkHttpUtils
                    .post()
                    .url(URL + "/user/register")
                    .addParams("account", account)
                    .addParams("username", username)
                    .addParams("phone", phone)
                    .addParams("password", password)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                            register_button.setText("注册");
                            Toast.makeText(RegisterActivity.this, "网络错误", Toast.LENGTH_LONG).show();
                        }

                        @Override
                        public void onResponse(String response) {
                            if(response.equals("true")) {
                                register_button.setText("注册");
                                Toast.makeText(RegisterActivity.this, "注册成功！", Toast.LENGTH_LONG).show();
                                SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putBoolean("LoginBool", true);
                                editor.putString("account", account);
                                editor.putString("username", username);
                                editor.putInt("sex", 0);
                                editor.putString("password",password);
                                editor.putString("phone", phone);
                                editor.apply();
                                Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();
                            }
                            else if(response.equals("account_false")){
                                register_button.setText("注册");
                                Toast.makeText(RegisterActivity.this, "注册失败，该账号已被注册过！", Toast.LENGTH_LONG).show();
                            }
                            else if(response.equals("phone_false")){
                                register_button.setText("注册");
                                Toast.makeText(RegisterActivity.this, "注册失败，该手机已被注册过！", Toast.LENGTH_LONG).show();
                            }
                            else{
                                register_button.setText("注册");
                                Toast.makeText(RegisterActivity.this, "注册失败！", Toast.LENGTH_LONG).show();
                            }
                        }
                    });
        }
        else{
            Toast.makeText(RegisterActivity.this, "两次输入密码不一致！", Toast.LENGTH_LONG).show();
        }
    }

    public void login_onClick(View view){
        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }
}
